﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
using Mirror;
public class Phone_RobotContol_Right : NetworkBehaviour
{

    public GameObject robotJoy;
    public float moveSpeed = 5;
    private float horizontalWrapMode, verticalMove;
    public Joystick joystick_right;
    public Joystick joystick_walk;

    public GameObject joy_walk;
    public GameObject joy_right;

    private CharacterController cc;

    [DllImport("user32.dll", EntryPoint = "keybd_event")]
    static extern void keybd_event(
        byte bVk,            //按键虚拟值，对应按键上的ascll码十进制值
        byte bScan,          //0
        int dwFlags,         //0 为按下，1按住，2释放
        int dwExtraInfo      //0
     );

    private void Awake()
    {
        cc = GetComponent<CharacterController>();
    }

    public override void OnStartLocalPlayer()
    {
        joy_walk.SetActive(true);
        joy_right.SetActive(true);
    }

    // Update is called once per frame
    void Update()
    {
        horizontalWrapMode = joystick_right.Horizontal * moveSpeed;
        verticalMove = joystick_walk.Vertical * moveSpeed;

        if(horizontalWrapMode == 0)//摇杆无运动时，模拟松开按键
        {
            keybd_event(65, 0, 2, 0);//模拟A键松开
            keybd_event(68, 0, 2, 0);//模拟D键松开
        }
        if (horizontalWrapMode > 0)//摇杆向右运动时，向右旋转
        {
            keybd_event(68, 0, 1, 0);//模拟A键按住
            print("A");
        }
        if (horizontalWrapMode < 0)//摇杆向左运动时，向左旋转
        {
            keybd_event(65, 0, 1, 0);//模拟D键按住
        }
        if (verticalMove == 0)//模拟W、S键松开
        {
            keybd_event(87, 0, 2, 0);
        }
        if (verticalMove != 0)//模拟W、S键按下
        {
            keybd_event(87, 0, 1, 0);
        }
        //dir = transform.forward * verticalMove;

        //transform.Rotate(0, horizontalWrapMode / 5, 0);
        

        //cc.Move(dir * Time.deltaTime);
        if(Input.GetKeyDown(KeyCode.A))
        {
            //print("A");
        }
    }
}
